package com.lry.thompson;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 若干dfa节点构成的一个mini dfa节点
 */
public class MiniDfa {

    public int groupNum;
    private List<Dfa> dfaList;
    private Set<Dfa> tobeRemove;

    public MiniDfa(int groupNum){
        this.groupNum = groupNum;
        this.dfaList = new ArrayList<>();
        this.tobeRemove = new HashSet<>();
    }

    public void addDfa(Dfa dfa){
        if(dfa==DfaManager.start){
            MiniDfaManager.start = this;
        }
        dfaList.add(dfa);
    }
    public Dfa getDfaByStateNum(int dfaStateNum){
        for (Dfa dfa : dfaList) {
            if(dfa.stateNum==dfaStateNum){
                return dfa;
            }
        }
        return null;
    }

    public Dfa getDfa(int index){
        if(index>=dfaList.size()){
            return null;
        }
        return dfaList.get(index);
    }

    public void tobeRemove(Dfa next) {
        tobeRemove.add(next);
    }

    public void commitRemove(){
        dfaList.removeAll(tobeRemove);
        tobeRemove.clear();
    }

    public boolean isEnd() {
        for (Dfa dfa : dfaList) {
            if(dfa.accepted){
                return true;
            }
        }
        return false;
    }
}
